<script setup>
import { useEditorStore, useModalStore, useDataStore } from '@/store'
import { useMessage } from 'naive-ui'
import { inject } from 'vue'
import { EDITOR_STATE } from '@/constant/editor.state.js'

const editorStore = useEditorStore()
const modalStore = useModalStore()
const dataStore = useDataStore()
const message = useMessage()

// 通过 provide/inject 获取 coordinateCanvas 实例
const coordinateCanvasRef = inject('coordinateCanvasRef')

const changeEditorMode = () => {
	editorStore.toggleMode()
	// 直接调用 canvas 方法
	if (coordinateCanvasRef?.value) {
		coordinateCanvasRef.value.changeMode(editorStore.mode)

		message.success('进入' + editorStore.mode + '模式')
	}
}

const setDrawMode = (drawMode) => {
	editorStore.setDrawMode(drawMode)
	// 直接调用 canvas 方法
	if (coordinateCanvasRef?.value) {
		coordinateCanvasRef.value.changeMode(drawMode)
	}
	message.success('进入' + drawMode + '模式')
}

const importData = () => {
	modalStore.openImportModal()
}

const captureScreen = () => {
	if (coordinateCanvasRef?.value) {
		coordinateCanvasRef.value.captureChart()
	}
}

const clearCanvas = () => {
	modalStore.openClearCanvasModal()
}

const downloadExampleFile = () => {
	const baseUrl = window.location.origin
	const url = new URL(baseUrl + '/example/level1.json')

	const element = document.createElement('a')

	element.href = url
	element.download = 'example.json'
	element.click()
}

const areaFilter = () => {
	modalStore.openAreaModal()
}

const enterInputMode = () => {
	editorStore.toggleInputMode()
	modalStore.toggleInputPointModal()
}

</script>

<template>
	<div class="tool-bar-continer">
		<div class="tool-bar-left">
		</div>

		<div class="tool-bar-center">
			<div class="editor-container">
				<div class="btn-item-box" @click="changeEditorMode">
					<button type="button" class="base-btn bg-cover change-mode-btn"></button>
					<div>{{ editorStore.mode === EDITOR_STATE.PREVIEW ? '编辑' : '预览' }}</div>
				</div>

								<!-- 编辑模式子工具栏 -->
				<div class="sub-toolbar" v-if="editorStore.mode !== EDITOR_STATE.PREVIEW">
					<div class="sub-btn-item"
						:class="{ active: editorStore.mode === EDITOR_STATE.DRAW_POINT }"
						@click="setDrawMode(EDITOR_STATE.DRAW_POINT)">
						<button type="button" class="sub-btn bg-cover draw-point-btn"></button>
						<div>画点</div>
					</div>

					<div class="sub-btn-item"
						:class="{ active: editorStore.mode === EDITOR_STATE.DRAW_LINE }"
						@click="setDrawMode(EDITOR_STATE.DRAW_LINE)">
						<button type="button" class="sub-btn bg-cover draw-line-btn"></button>
						<div>画线</div>
					</div>

					<div class="sub-btn-item"
						:class="{ active: editorStore.mode === EDITOR_STATE.DRAW_POLYGON }"
						@click="setDrawMode(EDITOR_STATE.DRAW_POLYGON)">
						<button type="button" class="sub-btn bg-cover draw-polygon-btn"></button>
						<div>画区域</div>
					</div>
				</div>
			</div>

			<div class="btn-item-box" @click="importData">
				<button type="button" class="base-btn bg-cover file-import-btn"></button>
				<div>导入</div>
			</div>

			<div class="btn-item-box" @click="areaFilter">
				<button type="button" class="base-btn bg-cover show-area-btn"></button>
				<div>区域</div>
			</div>

			<div class="btn-item-box" @click="captureScreen">
				<button type="button" class="base-btn bg-cover screen-shot-btn"></button>
				<div>截图</div>
			</div>

			<div class="btn-item-box" @click="enterInputMode">
				<button type="button" class="base-btn bg-cover input-mode-btn"></button>
				<div>输入</div>
			</div>

			<div class="btn-item-box" @click="clearCanvas">
				<button type="button" class="base-btn bg-cover clear-canvas-btn"></button>
				<div>清空</div>
			</div>

			<div class="btn-item-box" @click="downloadExampleFile">
				<button type="button" class="base-btn bg-cover download-example-btn"></button>
				<div>示例</div>
			</div>
		</div>

		<div class="tool-bar-right">
		</div>
	</div>
</template>

<style lang="scss" scoped>
.tool-bar-continer {
	display: flex;
	justify-content: space-between;
	align-items: flex-start;
	width: 100vw;
	min-height: 60px;
	background-color: #eee;
	border-bottom: 1px solid #ccc;
	position: relative;
	z-index: 1;
	margin: 0 auto;
	padding-top: 10px;


	.tool-bar-left {
		display: flex;
		width: 40vw;
	}

	.tool-bar-center {
		display: flex;
		justify-content: space-around;
		align-items: flex-start;
		position: relative;
		flex-wrap: nowrap;

		.btn-item-box {
			width: 60px;
			height: 100%;
			display: flex;
			align-items: center;
			justify-content: center;
			flex-direction: column;
			flex-wrap: nowrap;
			font-size: 14px;
			box-sizing: border-box;
			padding: 5px;
			cursor: pointer;

			&:hover {
				background-color: #ccc;

				.base-btn {
					background-color: #ccc;
				}
			}

			.base-btn {
				width: 30px;
				height: 30px;
				border: none;
				cursor: pointer;
			}

			.change-mode-btn {
				background-image: url('@/assets/svg/edit.svg');
			}

			.file-import-btn {
				background-image: url('@/assets/svg/import.svg');
			}

			.screen-shot-btn {
				background-image: url('@/assets/svg/screen-shot.svg');
			}

			.clear-canvas-btn {
				background-image: url('@/assets/svg/clear.svg');
			}

			.input-mode-btn {
				background-image: url('@/assets/svg/input.svg');
			}

			.show-area-btn {
				background-image: url('@/assets/svg/area.svg');
			}

			.download-example-btn {
				background-image: url('@/assets/svg/download-file.svg');
			}
		}

		.editor-container {
			position: relative;
			display: flex;
			flex-direction: column;
			align-items: center;
		}

		.sub-toolbar {
			position: absolute;
			top: 100%;
			left: 50%;
			transform: translateX(-50%);
			background-color: #fff;
			border: 1px solid #ccc;
			border-radius: 4px;
			padding: 8px;
			display: flex;
			gap: 8px;
			box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
			z-index: 10;
			margin-top: 4px;
			box-sizing: border-box;

			.sub-btn-item {
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 4px 8px;
				border-radius: 4px;
				cursor: pointer;
				transition: all 0.2s ease;
				width: 50px;

				&:hover {
					background-color: #f5f5f5;
				}

				&.active {
					background-color: #e6f7ff;
					border: 1px solid #1890ff;

					.sub-btn {
						background-color: #1890ff;
					}
				}

				.sub-btn {
					width: 24px;
					height: 24px;
					border: none;
					cursor: pointer;
					border-radius: 4px;
					margin-bottom: 4px;
				}

				div {
					font-size: 12px;
					color: #333;
				}

				.draw-point-btn {
					background-image: url('@/assets/svg/draw-point.svg');
					background-size: 16px;
					background-position: center;
					background-repeat: no-repeat;
				}

				.draw-line-btn {
					background-image: url('@/assets/svg/draw-line.svg');
					background-size: 16px;
					background-position: center;
					background-repeat: no-repeat;
				}

				.draw-polygon-btn {
					background-image: url('@/assets/svg/draw-area.svg');
					background-size: 16px;
					background-position: center;
					background-repeat: no-repeat;
				}
			}
		}
	}

	.tool-bar-right {
		display: flex;
		width: 40vw;
		height: 100%;
	}

	.bg-cover {
		background-size: cover;
	}
}
</style>